package com.android.allwinner.newaw360.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import java.util.HashMap;
import java.util.List;

import com.android.allwinner.newaw360.R;
import com.android.allwinner.newaw360.media.MediaFileInfo;
import com.android.allwinner.newaw360.utils.LogUtil;


/**
 * Created by Administrator on 2017/8/1.
 */

public class FileAdapter extends BaseAdapter{
    private static final String TAG = FileAdapter.class.getSimpleName();
    private Context mContext;
    private List<MediaFileInfo> mFileInfos;
    private static HashMap<Integer, Boolean> mSaveSelect;

    public FileAdapter(Context context) {
        mContext = context;
        mSaveSelect = new HashMap<Integer, Boolean>();
    }

    @Override
    public int getCount() {
        if (mFileInfos == null){
            return 0;
        }
        return mFileInfos.size();
    }

    public void mClearSaveSelect(){
        mSaveSelect.clear();
    }

    public void all_setSelected(){
        for(int i=0;i<getCount();i++){
            mFileInfos.get(i).setSelected(true);
            mSaveSelect.put(i,true);
        }
    }

    public void all_Cancel_Select(){
        for(int i=0;i<getCount();i++){
            mFileInfos.get(i).setSelected(false);
            mSaveSelect.remove(i);
        }
    }
    @Override
    public MediaFileInfo getItem(int position) {
        if(mFileInfos == null){
            return null;
        }
        return mFileInfos.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null){
            viewHolder = new ViewHolder();
            LayoutInflater inflater = LayoutInflater.from(mContext);
            convertView = inflater.inflate(R.layout.file_item,null);
            viewHolder.indexTextView = (TextView) convertView.findViewById(R.id.index_tv);
            viewHolder.dateTextView = (TextView) convertView.findViewById(R.id.date_tv);
            viewHolder.timeTextView = (TextView) convertView.findViewById(R.id.time_tv);
          /*  viewHolder.durationTextView = (TextView) convertView.findViewById(R.id.duration_tv);*/
            viewHolder.selectedCheckBox = (CheckBox) convertView.findViewById(R.id.select_cb);
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder)convertView.getTag();
        }
        if(mFileInfos != null){
            viewHolder.indexTextView.setText("" + position);
            viewHolder.dateTextView.setText(mFileInfos.get(position).getDate());
            viewHolder.timeTextView.setText(mFileInfos.get(position).getTime());
          /*  viewHolder.durationTextView.setText(mFileInfos.get(position).getDuration());*/
            viewHolder.selectedCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if(isChecked){
                        mSaveSelect.put(position, true);
                    }else {
                        mSaveSelect.remove(position);
                    }
                    if (mSaveSelect.get(position) != null) {
                        mFileInfos.get(position).setSelected(true);
                    } else {
                        mFileInfos.get(position).setSelected(false);
                    }
                }
            });
            viewHolder.selectedCheckBox.setChecked(mSaveSelect.get(position) == null ? false : true);
        }
        return convertView;
    }
    class ViewHolder{
        TextView indexTextView;
        TextView dateTextView;
        TextView timeTextView;
        /*TextView durationTextView;*/
        CheckBox selectedCheckBox;
    }

    private boolean mediaFileIsEmpty(){
        if (mFileInfos == null || mFileInfos.size() == 0){
            return true;
        }
        return false;
    }
    public void setMediaFiles(List<MediaFileInfo> fileInfos){
        mFileInfos = fileInfos;
        if (mediaFileIsEmpty()){
            notifyDataSetChanged();
            return;
        }
        for (MediaFileInfo fileInfo : fileInfos){
            fileInfo.setSelected(false);
        }
        notifyDataSetChanged();
    }
}
